package com.open.system.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.open.system.domain.LoanApplyDO;
import com.open.system.domain.LoanUserDO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.open.system.domain.LoanApplyLogDO;
import com.open.system.domain.LoanFileDO;
import com.open.system.service.LoanAdminLogService;
import com.open.system.service.LoanApplyLogService;
import com.open.system.service.LoanFileService;
import com.open.common.controller.BaseController;
import com.open.common.utils.PageUtils;
import com.open.common.utils.Query;
import com.open.common.utils.R;

/**
 * 文件管理
 */
 
@Controller
@RequestMapping("/system/loanFile")
public class LoanFileController extends BaseController {
	@Autowired
	private LoanFileService loanFileService;
	
	@Autowired
	private LoanApplyLogService loanApplyLogService;
	
	@Autowired
	private LoanAdminLogService loanAdminLogService;
	
	@GetMapping()
	@RequiresPermissions("system:loanFile:loanFile")
	String LoanFile(){
	    return "system/loanFile/loanFile";
	}

	@GetMapping("/contract")
	@RequiresPermissions("system:loanFile:loanFile")
	String contract(){
	    return "system/loanFile/contract";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("system:loanFile:loanFile")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//--操作日志--开始--
		String shopId=getUser().getEmail();
		String adminName=getUser().getName();
		String Module="文件图片";
		String detail="查看了文件，图片列表";
		loanAdminLogService.saveLog(shopId, adminName, Module, detail);
		//--操作日志--结束--
		if(shopId!=null && shopId!="" && !"1".equals(shopId)){
			params.put("shopId",shopId);
		}
		//查询列表数据
        Query query = new Query(params);
        
        List<LoanFileDO> loanFileList=null;
        int total=0;
        if("1".equals(shopId)){
        	loanFileList= loanFileService.list(query);
        	total = loanFileService.count(query);
        }else {
        	loanFileList = loanFileService.list2(query);
    		total = loanFileService.count2(query);
		}
		PageUtils pageUtils = new PageUtils(loanFileList, total);
		return pageUtils;
	}

	@ResponseBody
	@GetMapping("/contractList")
	@RequiresPermissions("system:loanFile:loanFile")
	public PageUtils contractList(@RequestParam Map<String, Object> params){
		//--操作日志--开始--
		String shopId=getUser().getEmail();
		String adminName=getUser().getName();
		String Module="文件图片";
		String detail="查看了文件，图片列表";
		loanAdminLogService.saveLog(shopId, adminName, Module, detail);
		//--操作日志--结束--
		if(shopId!=null && shopId!="" && !"1".equals(shopId)){
			params.put("shopId",shopId);
		}
		//查询列表数据
        Query query = new Query(params);
		query.put("remark1","3");
        List<LoanFileDO> loanFileList=null;
        int total=0;
        if("1".equals(shopId)){
        	loanFileList= loanFileService.list(query);
        	total = loanFileService.count(query);
        }else {
        	loanFileList = loanFileService.list2(query);
    		total = loanFileService.count2(query);
		}
		PageUtils pageUtils = new PageUtils(loanFileList, total);
		return pageUtils;
	}

	@GetMapping("/add")
	@RequiresPermissions("system:loanFile:add")
	String add(){
	    return "system/loanFile/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("system:loanFile:edit")
	String edit(@PathVariable("id") Long id,Model model){
		LoanFileDO loanFile = loanFileService.get(id);
		model.addAttribute("loanFile", loanFile);
	    return "system/loanFile/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("system:loanFile:add")
	public R save( LoanFileDO loanFile){
		loanFile.setCreateTime(new Date());
		if(loanFileService.save(loanFile)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("system:loanFile:edit")
	public R update( LoanFileDO loanFile){
		loanFileService.update(loanFile);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("system:loanFile:remove")
	public R remove( Long id){
		if(loanFileService.remove(id)>0){
			//--操作日志--开始--
			String shopId=getUser().getEmail();
			String adminName=getUser().getName();
			String Module="文件图片";
			String detail="删除了文件或者图片【"+id+"】";
			loanAdminLogService.saveLog(shopId, adminName, Module, detail);
			//--操作日志--结束--
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("system:loanFile:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		loanFileService.batchRemove(ids);
		return R.ok();
	}


	@ResponseBody
	@RequestMapping("/noPass")
	@RequiresPermissions("system:loanFile:loanFile")
	public R noPass(LoanFileDO loanFile){

		if(loanFileService.get(loanFile.getId()).getCheckstatus() != null){
			return R.error(201,"合同不是未审核状态，不允许操作！");
		}

		LoanFileDO loanFileDONew = new LoanFileDO();
		loanFileDONew.setCheckstatus("2");
		loanFileDONew.setId(loanFile.getId());

		loanFileService.update(loanFileDONew);

		return R.ok();
	}

	@ResponseBody
	@RequestMapping("/pass")
	@RequiresPermissions("system:loanFile:loanFile")
	public R Pass(LoanFileDO loanFile){

		if(loanFileService.get(loanFile.getId()).getCheckstatus() != null){
			return R.error(201,"合同不是未审核状态，不允许操作！");
		}

		LoanFileDO loanFileDONew = new LoanFileDO();
		loanFileDONew.setCheckstatus("1");
		loanFileDONew.setId(loanFile.getId());

		loanFileService.update(loanFileDONew);

		return R.ok();
	}


}
